package com.test5.liang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.generic.ARRAYLENGTH;
import com.test5.liang.pojo.User;
import com.test5.liang.pojo.examination;
import com.test5.liang.pojo.registerLog;
import com.test5.liang.service.UserService;
import com.test5.liang.service.examinationService;
import com.test5.liang.service.examtypeService;
import com.test5.liang.service.registerLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class adminController {

    @Autowired
    registerLogService registerLogService;

    @Autowired
    UserService userService;

    @Autowired
    examinationService examService;

    @Autowired
    com.test5.liang.service.examtypeService examtypeService;



    /**
     * @Author huangzhenliang
     * @Description //TODO 黄臻良
     * @Date 21:33 2021/12/15
     * @Param
     * @return
     **/
    @RequestMapping("/adminInfo")
    public String adminInfo(@RequestParam("username")String username,Model model){

        User user = userService.findUserByName(username);
        model.addAttribute("user",user);
        System.out.println("****************");
        System.out.println(user.toString());
        String role;
        if (user.getUserRole()==1){
            role = "用户";
        }else {
            role = "管理员";
        }
        model.addAttribute("username",username);
        model.addAttribute("role",role);
        return "adminpart/adminInfo";
    }

     /**
      * @Author huangzhenliang
      * @Description //TODO 黄臻良
      * @Date 23:32 2021/12/15
      * @Param
      * @return
      **/
     @RequestMapping("/usermanage")
     public String usermanage(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum,
             @RequestParam("username")String username,Model model){


         PageHelper.startPage(pagenum, 8);  //pagenum：页数，pagesize:每页的信息数

         List<User> users = userService.selectAllUser();//调用业务层查询方法

         PageInfo<User> pageInfo = new PageInfo<>(users); //得到分页结果对象

         model.addAttribute("pageInfo", pageInfo);  //携带分页结果信息
         model.addAttribute("username", username);

         return "adminpart/usermanage";
     }




     /**
      * @Author huangzhenliang
      * @Description //TODO 黄臻良
      * @Date 12:54 2021/12/21
      * @Param
      * @return
      **/
     @RequestMapping("/myExamHistory")
     public String myExamHistory(@RequestParam(value = "typeId",required = false)Integer typeId,
                                 @RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum,
                                 @RequestParam(required = false,value = "checkname")String checkname,
                                 Model model, HttpSession httpSession) {

         //获取所有的模式选项
         List<examination> examtypeList = examtypeService.selectAllExamtype();
         model.addAttribute("examtypeList",examtypeList);
         //获取userid
         User user = null;
         examination examination = new examination();
         if (checkname!=null&&!checkname.equals("")){
             user = userService.findUserByName(checkname);
             if (user!=null){
                 examination.setUserId(user.getUserId());
             }else {
                 examination.setUserId(-1l);
             }
         }
         //获取该用户各种成绩记录
         if (typeId!=null){
             examination.setExamTypeId(typeId);
         }
         PageHelper.startPage(pagenum, 8);  //pagenum：页数，pagesize:每页的信息数
         List<examination> examinations = examService.selectExamination(examination);
         PageInfo<examination> pageInfo = new PageInfo<>(examinations); //得到分页结果对象
         model.addAttribute("examinations",examinations);
         model.addAttribute("pageInfo", pageInfo);  //携带分页结果信息

         //查询最近的7次成绩结果返回
         //examination examination1 = new examination();
         //examination1.setUserId(user.getUserId());
         List<examination> examinations1 = examService.selectExamination(examination);
         ArrayList<Integer> list = new ArrayList<>();
         int index = Math.min(examinations1.size(), 7);
         for (int i=0;i<index;i++){
             list.add(examinations1.get(i).getExamScore());
         }
         //查询不同模式的次数
         List<examination> examinations2 = examService.selectExamtypeCount(examination);
         ArrayList<String> listname = new ArrayList<>();
         ArrayList<Integer> listvalue = new ArrayList<>();

         for (com.test5.liang.pojo.examination examination2 : examinations2) {
             listname.add(examination2.getExamTypename());
             listvalue.add(examination2.getTypenum());
         }
         model.addAttribute("listname",listname);
         model.addAttribute("listvalue",listvalue);

         model.addAttribute("ExamScore",list);
         return "adminpart/userExamHistory";
     }





}